﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RecommenderSystem;

namespace RecommenderSystem.misc
{
    public class Cache
    {
        private Dictionary<string, Dictionary<string, double>> cache;
        private readonly int CACHE_INITIAL_SIZE = 500;
        public Cache()
        {
            cache = new Dictionary<string, Dictionary<string, double>>(CACHE_INITIAL_SIZE);
        }


        public bool isCached(string item1, string item2)
        {
            return (cache.ContainsKey(item1) && cache[item1].ContainsKey(item2));
        }

        public bool addToCache(string item1, string item2, double value)
        {

            if (cache.ContainsKey(item1))
            {
                if (!cache[item1].ContainsKey(item2))
                {
                    cache[item1].Add(item2, value);
                    return true;
                }
                cache[item1][item2] = value;
            }
            else
            {
                cache.Add(item1, new Dictionary<string, double>(CACHE_INITIAL_SIZE));
                cache[item1].Add(item2, value);
            }

            return true;
        }

        public double retrieveFromCache(string userId, string otherUserId)
        {
            if (cache.ContainsKey(userId) && cache[userId].ContainsKey(otherUserId))
            {
                return cache[userId][otherUserId];
            }
            return -1;
        }
    }
}
